home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 1 Issue 2
/
PDCD-1 - Issue 02.iso
/
_utilities
/
utilities
/
001
/
_au2snd
/
!au2snd
/
c
/
au2snd
Wrap
Text File
|
1992-07-30
|
6KB
|
266 lines
/************************************************************************/
/* au2snd.c - Convert sampled audio files in uLAW format to signed */
/* eight bit ones for Archimedes. */
/* Send comments to: jhowat@waikato.ac.nz */
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "os.h"
#include "swis.h"
#define DEFAULT_SKIP 32
#define SAMPLE_FILETYPE 0xD3C
/*** convert uLAW format ch into two's complement ***/
int cvtback(unsigned int ch)
{
int tmp ;
tmp = 15 - (ch & 0x0F) ;
switch(ch & 0x70)
{
case 0x70 : tmp = 2 * tmp ;
break ;
case 0x60 : tmp = 4 * tmp + 32 ;
break ;
case 0x50 : tmp = 8 * tmp + 96 ;
break ;
case 0x40 : tmp = 16 * tmp + 224 ;
break ;
case 0x30 : tmp = 32 * tmp + 480 ;
break ;
case 0x20 : tmp = 64 * tmp + 992 ;
break ;
case 0x10 : tmp = 128 * tmp + 2016 ;
break ;
case 0x00 : tmp = 256 * tmp + 4064 ;
break ;
}
if (!(ch & 0x80)) tmp = -tmp ;
return (tmp/4);
}
void usage(void)
{
fprintf(stderr, "Usage: au2snd <infile> [-s [<count>]] <outfile>\n\n");
fprintf(stderr, " The -s switch makes au2snd skip <count>\n");
fprintf(stderr, " bytes from the beginning of the '.au' file.\n");
fprintf(stderr, " If no count is given then %i is used.\n\n",DEFAULT_SKIP);
exit(1);
}
int main(argc, argv)
int argc;
char *argv[];
{
int ch;
FILE *infile, *outfile;
int amplif ;
int i;
int maxch, minch;
int skipflag;
int skipcount;
int file_length, new_length;
os_regset my_registers;
unsigned char *conv_buffer;
os_error *the_error;
fprintf(stderr, "\nau2snd: V2.00 Copyright (C) Jason Howat 25 June 1992\n");
fprintf(stderr, " Auto-amplification version\n\n");
if ((argc < 3) || (argc > 5))
{
usage();
}
if ((infile = fopen(argv[1], "rb")) == NULL)
{
fprintf(stderr,"au2snd: Error opening %s\n",argv[1]);
exit(1);
}
/* Read in the length of the file to be converted */
my_registers.r[0] = 5;
my_registers.r[1] = (int) argv[1];
os_swi(OS_File, &my_registers);
file_length = my_registers.r[4];
fprintf(stderr, "au2snd: Input file is %d byte", file_length);
if (file_length == 1)
{
fprintf(stderr, " long.\n");
}
else
{
fprintf(stderr, "s long.\n");
}
skipflag = FALSE;
skipcount = 0;
if (argc > 3)
{
if (strcmp(argv[2],"-s") != 0)
{
usage();
}
skipflag = TRUE;
skipcount = DEFAULT_SKIP;
if (argc == 5)
{
skipcount = atoi(argv[3]);
if (skipcount < 0)
{
usage();
}
}
}
if (file_length < skipcount)
{
fprintf(stderr, "au2snd: skipped past end-of-file!\n\n");
exit(1);
}
new_length = file_length - skipcount;
/*** create a buffer for conversion of data ***/
conv_buffer = malloc(new_length);
if (!conv_buffer)
{
fprintf(stderr, "au2snd: Error allocating buffer (enough memory? need %d)", new_length);
exit(1);
}
/*** move past 'skipcount' bytes and read file into buffer ***/
fseek(infile, skipcount, SEEK_SET);
fread(conv_buffer, sizeof(unsigned char), new_length, infile);
fclose(infile) ;
new_length = file_length - skipcount;
my_registers.r[0] = 11;
my_registers.r[1] = (int) argv[argc - 1];
my_registers.r[2] = SAMPLE_FILETYPE;
my_registers.r[4] = 0;
my_registers.r[5] = new_length;
fprintf(stderr,"au2snd: Output file %d byte", new_length + 1);
if ((new_length + 1) == 1)
{
fprintf(stderr, " long.\n");
}
else
{
fprintf(stderr, "s long.\n");
}
if ((the_error = os_swix(OS_File, &my_registers)) != NULL)
{
fprintf(stderr,"au2snd: Error creating %s\n Because: %s\n\n",
argv[argc - 1], the_error->errmess);
exit(1);
}
if ((outfile = fopen(argv[argc - 1], "rb+")) == NULL)
{
fprintf(stderr,"au2snd: Error opening %s\n",argv[argc - 1]);
exit(1);
}
fprintf(stderr, "au2snd: Skipping %d byte", skipcount);
if (skipcount == 1)
{
fprintf(stderr, "\n");
}
else
{
fprintf(stderr, "s\n");
}
/*** all the data is now in memory, so convert it ***/
minch = INT_MAX;
maxch = INT_MIN;
for(i = 0 ; i < new_length ; i++)
{
/*** convert uLAW to signed 8-bit ***/
ch = cvtback(conv_buffer[i]);
if (ch < minch)
{
minch = ch;
}
if (ch > maxch)
{
maxch = ch;
}
}
amplif = 1;
if (maxch != 0)
{
amplif = 127000 / maxch;
}
if (minch != 0)
{
if (((-128000) / minch) < amplif)
{
amplif = (-128000) / minch;
}
}
amplif = amplif - (1000 / 256);
fprintf(stderr, "au2snd: Using amplification of %d.%.3d\n", amplif/1000, amplif % 1000);
/*** cheap and nasty to get right sample rate ***/
fputc('}', outfile);
/*** go back to beginning, and write out conversion ***/
my_registers.r[0] = 1;
os_swi(Hourglass_Start, &my_registers);
for(i = 0 ; i < new_length ; i++)
{
/*** convert uLAW to signed 8-bit ***/
conv_buffer[i] = (char) (cvtback(conv_buffer[i]) * amplif / 1000);
if (!(i & 0x3ff))
{
my_registers.r[0] = (100 * i) / new_length;
os_swi(Hourglass_Percentage, &my_registers);
}
}
my_registers.r[0] = 100;
os_swi(Hourglass_Percentage, &my_registers);
fwrite(conv_buffer, sizeof(unsigned char), new_length, outfile);
os_swi(Hourglass_Off, &my_registers);
fclose(outfile) ;
fprintf(stderr, "\n\n");
}